<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货号" prop="productSn">
        <el-input
          v-model="queryParams.productSn"
          placeholder="请输入货号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="stock">
        <el-input
          v-model="queryParams.stock"
          placeholder="请输入库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键词" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌名称" prop="brandName">
        <el-input
          v-model="queryParams.brandName"
          placeholder="请输入品牌名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分类名称" prop="productCategoryName">
        <el-input
          v-model="queryParams.productCategoryName"
          placeholder="请输入商品分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="封面" align="center" prop="pic">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.picture_upload_method" :value="scope.row.pic"/>
        </template>
      </el-table-column>
      <el-table-column label="货号" align="center" prop="productSn" />
      <el-table-column label="删除状态" align="center" prop="deleteStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.delete_status" :value="scope.row.deleteStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="publishStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.listing_status" :value="scope.row.publishStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="新品状态" align="center" prop="newStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.new_status" :value="scope.row.newStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="推荐状态" align="center" prop="recommandStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.recommandStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="verifyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.review_status" :value="scope.row.verifyStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="销量" align="center" prop="sale" />
      <el-table-column label="原价" align="center" prop="price" />
      <el-table-column label="促销价格" align="center" prop="promotionPrice" />
      <el-table-column label="赠送成长值" align="center" prop="giftGrowth" />
      <el-table-column label="赠送积分" align="center" prop="giftPoint" />
      <el-table-column label="限制使用积分数" align="center" prop="usePointLimit" />
      <el-table-column label="副标题" align="center" prop="subTitle" />
      <el-table-column label="商品描述" align="center" prop="description" width="700px" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="库存预警值" align="center" prop="lowStock" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="商品重量" align="center" prop="weight" />
      <el-table-column label="是否为预购商品" align="center" prop="previewStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whether_or_not" :value="scope.row.previewStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="产品服务" align="center" prop="serviceIds" />
      <el-table-column label="促销开始时间" align="center" prop="promotionStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.promotionStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="促销结束时间" align="center" prop="promotionEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.promotionEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="限购数量" align="center" prop="promotionPerLimit" />
      <el-table-column label="促销类型" align="center" prop="promotionType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="封面" prop="pic">
          <file-upload v-model="form.pic"/>
        </el-form-item>
        <el-form-item label="货号" prop="productSn">
          <el-input v-model="form.productSn" placeholder="请输入货号" />
        </el-form-item>
        <el-form-item label="原价" prop="price">
          <el-input v-model="form.price" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="促销价格" prop="promotionPrice">
          <el-input v-model="form.promotionPrice" placeholder="请输入促销价格" />
        </el-form-item>
        <el-form-item label="赠送成长值" prop="giftGrowth">
          <el-input v-model="form.giftGrowth" placeholder="请输入赠送成长值" />
        </el-form-item>
        <el-form-item label="赠送积分" prop="giftPoint">
          <el-input v-model="form.giftPoint" placeholder="请输入赠送积分" />
        </el-form-item>
        <el-form-item label="限制使用积分数" prop="usePointLimit">
          <el-input v-model="form.usePointLimit" placeholder="请输入限制使用积分数" />
        </el-form-item>
        <el-form-item label="副标题" prop="subTitle">
          <el-input v-model="form.subTitle" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="库存预警值" prop="lowStock">
          <el-input v-model="form.lowStock" placeholder="请输入库存预警值" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="商品重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入商品重量" />
        </el-form-item>
        <el-form-item label="是否为预购商品" prop="previewStatus">
          <el-radio-group v-model="form.previewStatus">
            <el-radio
              v-for="dict in dict.type.whether_or_not"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="促销开始时间" prop="promotionStartTime">
          <el-date-picker clearable
                          v-model="form.promotionStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择促销开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="促销结束时间" prop="promotionEndTime">
          <el-date-picker clearable
                          v-model="form.promotionEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择促销结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="限购数量" prop="promotionPerLimit">
          <el-input v-model="form.promotionPerLimit" placeholder="请输入限购数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/product/product";

export default {
  name: "Product",
  dicts: ['review_status', 'new_status', 'delete_status', 'whether_or_not', 'sys_normal_disable', 'listing_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        productSn: null,
        stock: null,
        keywords: null,
        brandName: null,
        productCategoryName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        productSn: [
          { required: true, message: "货号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productCategoryId: null,
        feightTemplateId: null,
        productAttributeCategoryId: null,
        productBrandId: null,
        name: null,
        pic: null,
        productSn: null,
        deleteStatus: null,
        publishStatus: null,
        newStatus: null,
        recommandStatus: null,
        verifyStatus: null,
        sort: null,
        sale: null,
        price: null,
        promotionPrice: null,
        giftGrowth: null,
        giftPoint: null,
        usePointLimit: null,
        subTitle: null,
        description: null,
        stock: null,
        lowStock: null,
        unit: null,
        weight: null,
        previewStatus: null,
        serviceIds: [],
        keywords: null,
        albumPics: null,
        promotionStartTime: null,
        promotionEndTime: null,
        promotionPerLimit: null,
        promotionType: null,
        brandName: null,
        productCategoryName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.form.serviceIds = this.form.serviceIds.split(",");
        this.open = true;
        this.title = "修改商品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.serviceIds = this.form.serviceIds.join(",");
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品信息编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
